package com.keyinong.caizhengting.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.tools.HttpTools;
import com.keyinong.caizhengting.tools.ImageUtils;
import com.keyinong.caizhengting.tools.NetWorkUtil;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author：郑谦
 * @时间：2017-4-10 下午4:02:27
 * @类名：ErweimaActivity.java
 * @功能: 取餐二维码
 */

public class ErweimaActivity extends Activity {

    HttpTools mHttpTools = null;
    /**
     * 二维码
     */
    ImageView iv_erweima;
    /**
     * 条码
     */
    ImageView iv_tiaoma;
    /**
     * 转发
     */
    ImageButton ib_zhuanfa;
    /**
     * 返回
     */
    ImageButton ib_fanhui;
    /**
     * 名字
     */
    TextView tv_name;
    /**
     * 编号
     */
    TextView tv_bianhao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setWindowBrightness(WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL);
        setContentView(R.layout.activity_erweima);
        iv_erweima = (ImageView) findViewById(R.id.iv_erweima);
        iv_tiaoma = (ImageView) findViewById(R.id.iv_tiaoma);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_bianhao = (TextView) findViewById(R.id.tv_bianhao);
        ib_fanhui = (ImageButton) findViewById(R.id.ib_fanhui);
        ib_fanhui.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ib_zhuanfa = (ImageButton) findViewById(R.id.ib_zhuanfa);
        ib_zhuanfa.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
//				finish();
            }
        });
        getQRCode();
    }

    /**
     * 获取二维码
     */
    private void getQRCode() {
        if (!NetWorkUtil.isNetwork(getApplicationContext())) {
            Toast.makeText(getApplicationContext(), "当前网络不可用", Toast.LENGTH_SHORT).show();
            return;
        }
        SharedPreferences sp = getSharedPreferences("spReg", Context.MODE_PRIVATE);
        String token = sp.getString("data", "");
        String id = getIntent().getStringExtra("id");
        if (mHttpTools == null) {
            mHttpTools = new HttpTools(this);
        }

        mHttpTools.getCode(token, id, new JsonErweima());
    }

    /**
     * 设置当前窗口亮度
     *
     * @param brightness
     */
    private void setWindowBrightness(float brightness) {
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.screenBrightness = brightness;
        window.setAttributes(lp);
    }

    public class JsonErweima extends JsonHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            super.onSuccess(statusCode, headers, response);
            try {
                Log.i("zq", response.toString());
                boolean code = response.getBoolean("code");
                String msg = response.getString("msg");
                if (code) {
                    JSONObject object = response.getJSONObject("data");
                    String name = object.getString("name");
                    String dayno = object.getString("dayno");
                    String pic = object.getString("pic");
                    String barcode = object.getString("barcode");
                    tv_name.setText(name);
                    tv_bianhao.setText(dayno);
                    //ImageUtils.displayImage(getApplicationContext(), pic, iv_erweima, R.drawable.wupian1);
                    //ImageUtils.displayImage(getApplicationContext(), barcode, iv_tiaoma, R.drawable.wupian1);
                    Glide.with(ErweimaActivity.this)
                            .load(pic)
                            .into(iv_erweima);
                    Glide.with(ErweimaActivity.this)
                            .load(barcode)
                            .into(iv_tiaoma);
                } else {
                    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

}
